﻿#region
/* ===============================================
 * 作者：guopeng           时间：2012/2/1 21:15:09
 * 
 * 
 * 类描述： 商家类
 * 
 * =============================================== */
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jobs.Inf.Entity;
using Jobs.Inf.DataContext;
using Jobs.Domain.Sheet;
using Jobs.Inf.Protector;

namespace Jobs.Domain.User
{
    public class Advertiser : Inf.Entity.Advertiser
    {
        public User User { get; private set; }
        /// <summary>
        /// initType 初始化方式。
        /// 0：单代理公司初始化方式
        /// 1：以companyName方式初始化
        /// 2：以代理公司id的方式初始化
        /// </summary>
        private int initType { get; set; }
        /// <summary>
        /// 不指定某一商家，这里只是初始化一个商家角色
        /// </summary>
        /// <param name="agent">代理人名（用户名）</param>
        public Advertiser(string agent)
        {
            this.Agent = agent;
            this.User = new User(agent);
            this.initType = 0;
        }
        /// <summary>
        /// 指定以一个商家名为[companyName]的商家初始化一个商家角色
        /// </summary>
        /// <param name="agent"></param>
        /// <param name="companyName"></param>
        public Advertiser(string agent,string companyName)
        {
            this.Agent = agent;
            this.User = new User(agent);
            this.CompanyName = companyName;
            this.initType = 1;
        }
        /// <summary>
        /// 指定以一个商家id为[advertiserId]的商家初始化一个商家角色
        /// </summary>
        /// <param name="agent"></param>
        /// <param name="advertiserId"></param>
        public Advertiser(string agent, int advertiserId)
        {
            this.Agent = agent;
            this.User = new User(agent);
            this.Id = advertiserId;
            this.initType = 2;
        }

        /// <summary>
        /// 更加商家id获取商家信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Inf.Entity.Advertiser Get(int id)
        {
            using (Context ctx = new Context())
            {
                return ctx.Advertiser.FirstOrDefault(c => c.Id == id);
            }
        }

        /// <summary>
        /// 获取某一代理人的所有商家信息
        /// </summary>
        /// <param name="agent"></param>
        /// <returns></returns>
        public IList<Inf.Entity.Advertiser> Get(string agent)
        {
            using (Context ctx = new Context())
            {
                var item = from c in ctx.Advertiser
                           where c.Agent == agent
                           select c;
                return item.ToList();
            }
        }

        /// <summary>
        /// 为代理人添加一个商家信息
        /// </summary>
        /// <param name="companyName"></param>
        /// <param name="about"></param>
        /// <param name="location"></param>
        /// <param name="phone"></param>
        /// <param name="email"></param>
        /// <returns>如果添加成功，返回这个商家信息的id，否则返回-1</returns>
        public int New(string about, string location, string phone, string email)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Advertiser.Add(new Inf.Entity.Advertiser()
                {
                    About = about,
                    Agent = this.Agent,
                    CompanyName = this.CompanyName,
                    EMail = email,
                    Identification = false,
                    Location = location,
                    Phone = phone
                });
                if (item != null)
                {
                    return item.Id;
                }
                return -1;
            }
        }

        /// <summary>
        /// 更新商家信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="companyName"></param>
        /// <param name="about"></param>
        /// <param name="location"></param>
        /// <param name="phone"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool Refresh(int id,  string about, string location, string phone, string email)
        {
            using (Context ctx = new Context())
            {
                return ctx.Update<Inf.Entity.Advertiser>()
                    .Set(c => c.CompanyName, this.CompanyName)
                    .Set(c => c.About, about)
                    .Set(c => c.Location, location)
                    .Set(c => c.Phone, phone)
                    .Set(c => c.EMail, email)
                    .WhereSet(c => c.Id, WhereOperator.Equal, id)
                    .End()
                    .Excute() > 0;
            }
        }

        /// <summary>
        /// 商家的招募行为
        /// </summary>
        public Recruit Recruit
        {
            get
            {
                int advertiserId = -1;
                if (initType != 2)
                {
                    using (Context ctx = new Context())
                    {
                        if (initType == 1)
                        {
                            var adv = ctx.Advertiser.FirstOrDefault(c => c.Agent == this.Agent && c.CompanyName == this.CompanyName);
                            if (adv != null)
                            {
                                advertiserId = adv.Id;
                            }
                            else
                            {
                                throw new ContextException("为找到指定商家，请设置正确的代理人和商家名", ExceptionCode.OutOfDataBase);
                            }
                        }
                        else
                        {
                            var adv = ctx.Advertiser.FirstOrDefault(c => c.Agent == this.Agent);
                            if (adv != null)
                            {
                                advertiserId = adv.Id;
                            }
                            else
                            {
                                throw new ContextException("为找到指定商家，请设置正确的代理人和商家名", ExceptionCode.OutOfDataBase);
                            }
                        }
                    }
                }

                return new Recruit(this.Agent, advertiserId);
            }
        }
    }
}
